<template>
  <el-dialog title="新增员工" :visible.sync="showDialog" @close="handleClose">
    <!-- 表单 -->
    <el-form ref="addEmployeeRef" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="姓名" prop="username">
        <el-input v-model="formData.username" style="width: 100%" placeholder="请输入姓名" />
      </el-form-item>
      <el-form-item label="手机" prop="mobile">
        <el-input v-model="formData.mobile" style="width: 100%" placeholder="请输入手机号" />
      </el-form-item>
      <el-form-item label="入职时间" prop="timeOfEntry">
        <el-date-picker v-model="formData.timeOfEntry" type="date" style="width: 100%" placeholder="请选择入职时间" />
      </el-form-item>
      <el-form-item label="聘用形式" prop="formOfEmployment">
        <el-select v-model="formData.formOfEmployment" style="width: 100%" placeholder="请选择">
          <el-option v-for="item in EmployeeEum.hireType" :key="item.id" :label="item.value" :value="item.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="工号" prop="workNumber">
        <el-input v-model="formData.workNumber" style="width: 100%" placeholder="请输入工号" />
      </el-form-item>
      <el-form-item label="部门" prop="departmentName">
        <el-input v-model="formData.departmentName" style="width: 100%" placeholder="请选择部门" @focus="getDepartmentsTree" />
        <!-- 放置一个tree组件 -->
        <el-tree v-if="showTree" v-loading="loading" :data="treeData" default-expand-all :props="{ label: 'name' }" @node-click="selectNode" />
      </el-form-item>
      <el-form-item label="转正时间" prop="correctionTime">
        <el-date-picker v-model="formData.correctionTime" type="date" style="width: 100%" placeholder="请选择转正时间" />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="handleClose">取消</el-button>
          <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getEmployeeList, addEmployee } from '@/api/employees'
import EmployeeEum from '@/api/constant/employees'
// import { getDepartments } from '@/api/departments'
// 导入无限极分类的工具函数
// import { tranListToTreeData } from '@/utils'
import { transformDepartment } from '@/mixins'
export default {
  // 混入
  mixins: [transformDepartment],
  data() {
    // 手机号码的唯一性验证
    async function checkMobile(rule, value, callback) {
      const { rows } = await getEmployeeList({ page: 1, size: 1000000 })
      const isRepeat = rows.some(item => item.mobile === value)
      isRepeat ? callback(new Error('手机号码重复')) : callback()
    }
    return {
      // 控制对话框的显示和隐藏
      showDialog: false,
      // 定义员工的枚举数据
      EmployeeEum,
      formData: {
        username: '',
        mobile: '',
        formOfEmployment: '', // 要求是id
        workNumber: '',
        departmentName: '',
        timeOfEntry: '',
        correctionTime: '',
        inServiceStatus: 1 // 账号状态
      },
      rules: {
        username: [
          { required: true, message: '用户姓名不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 4,
            message: '用户姓名为1-4位',
            trigger: 'blur'
          }
        ],
        mobile: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '手机号格式不正确',
            trigger: 'blur'
          },
          { validator: checkMobile, trigger: 'blur' }
        ],
        formOfEmployment: [{ required: true, message: '聘用形式不能为空', trigger: 'blur' }],
        workNumber: [
          { required: true, message: '工号不能为空', trigger: 'blur' },
          { pattern: /^[1-9]\d*$/, message: '工号必须是正整数' }
          // 工号的唯一性验证（省略，自行完成）
        ],
        departmentName: [{ required: true, message: '部门不能为空', trigger: 'change' }],
        timeOfEntry: [{ required: true, message: '入职时间', trigger: 'blur' }]
      }
      /* // 定义部门树形数据
      treeData: [],
      // 是否显示加载进度
      loading: false,
      // 树形组件的显示和隐藏
      showTree: false */
    }
  },
  methods: {
    // 打开对话框
    open() {
      this.showDialog = true
    },
    // 对话框关闭的处理事件
    handleClose() {
      this.showDialog = false
      // 重置表单
      this.$refs.addEmployeeRef.resetField()
    },
    /* // 获取部门的树形数据
    async getDepartments() {
      this.loading = true
      const { depts } = await getDepartments()
      this.loading = false
      // 使用无限极分类的方法处理列表数据为树形数据
      this.treeData = tranListToTreeData(depts, '')
    },
    // 打开部门列表的树形组件
    async getDepartmentsTree() {
      // 获取完数据后再显示树形组件
      await this.getDepartments()
      this.showTree = true
    }, */
    // 点击树形节点，选择对应的节点作为部门数据
    selectNode(data) {
      // 给部门数据赋值
      this.formData.departmentName = data.name
      // 关闭树形组件
      this.showTree = false
    },
    // 确认添加
    btnOk() {
      this.$refs.addEmployeeRef.validate(async valid => {
        if (!valid) return false
        try {
          // 验证成功
          await addEmployee(this.formData)
          this.$message.success('添加员工成功')
          // 重新获取数据
          this.$emit('get-new-employee')
        } catch {
          // todo
        } finally {
          this.showDialog = false
        }
      })
    }
  }
}
</script>

<style></style>
